package com.crazy.security;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
 * 图片验证码--自定义标签
 * 
 * <pre>
 * &lt;%@ taglib uri="/image-validate-code" prefix="img" %&gt;
 * &lt;img:img action="actionName"/&gt;
 * </pre>
 * @author zhang weiwei
 */
@SuppressWarnings("serial")
public class ValidateCodeTag extends TagSupport {
	private String action;// 地址
	private String border = "1";// 边框
	@Override
	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			out.println("<script type=\"text/javascript\">");
			out.println("function changeValidCode(obj) {");
			out.println("obj.src=\"" + this.action + "?\"+Math.random();");
			out.println("}");
			out.println("</script>");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			out.print("<img alt=\"图片正在加载中...\" title=\"看不清，换一张！\" src=\""
					+ this.action + "\" style=\"cursor:pointer;\" border=\""
					+ this.border + "\" onclick=\"changeValidCode(this)\"/>");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	/**
	 * @param action 要设置的 action
	 */
	public void setAction(String action) {
		this.action = action;
	}
	/**
	 * @param border 要设置的 border
	 */
	public void setBorder(String border) {
		this.border = border;
	}
}
